﻿define(function (require, exports, module) {

    /**
     * Bookmark JSON data format
     */
    var BookmarkJSONDataFormat =
    {
        bookmarks: "bmks",
        parentIndex: "parent",
        curIndex: "cur",
        childs: "childs",
        title: "ttl",
        destInfo: "dest"
    };
    WebPDF.PDFData.BookmarkJSONDataFormat = BookmarkJSONDataFormat;

    /**
     * PDF bookmark info
     * @param parentIndex index of parent bookmark node.
     * @param currentIndex index of current node.
     * @param title title of current node.
     * @param {WebPDF.PDFData.PDFDestination} destinationInfo destination of current node.
     * @constructor
     */
    function PDFBookmarkInfo(parentIndex, currentIndex, title, destinationInfo) {
        this._nParentID = parentIndex;
        this.currentIndex = currentIndex;
        this.destinationInfo = destinationInfo;
        this.title = title;
    }

    /**
     * Bookmark object
     * @param bookmarkInfo bookmark info in JSON format.
     * @constructor
     */
    var PDFBookmark = function (bookmarkInfo) {
        /**
         * bookmark information
         * @type {PDFBookmarkInfo}
         */
        this._bookmarkInfo = bookmarkInfo;
        /**
         * bookmark node list.
         * @type {Array.<PDFBookmark>}
         * @private
         */
        this._childNodeList = [];
    };

    PDFBookmark.prototype = {
        /**
         * Add child node of bookmark
         * @param {PDFBookmark} childNode child node of bookmark
         */
        addChildBookmark : function (childNode) {
            this._childNodeList.push(childNode);
        },
        /**
         * Get count of child node.
         * @returns {Array.length|*}
         */
        getChildCount : function () {
            return this._childNodeList.length;
        },
        /**
         * Get child bookmark node by index
         * @param index index
         * @returns {PDFBookmark}
         */
        getChild : function (index) {
            return this._childNodeList[index];
        },
        /**
         * Get bookmark info.
         * @returns {PDFBookmarkInfo}
         */
        getBookmarkInfo : function () {
            return this._bookmarkInfo;
        },
        /**
         * Parse bookmarks information to bookmark nodes.
         * @param jsonBookmarksInfo bookmarks information in JSON format.
         */
        parserBookmarksInfo : function (jsonBookmarksInfo) {
            var bookmarks = jsonBookmarksInfo[BookmarkJSONDataFormat.bookmarks];
            if (!bookmarks)  {
                return;
            }
            for (var i = 0; i < bookmarks.length; i++) {
                var jsonBookmark = bookmarks[i];
                if (jsonBookmark[BookmarkJSONDataFormat.parentIndex] === 0) {
                    var node = this._addBookmarkNode(this, jsonBookmark, i);
                    var childList = jsonBookmark[BookmarkJSONDataFormat.childs];
                    if (childList) {
                        var size = childList.length;
                        for (var k = 0; k < size; k++) {
                            var index = childList[k] - 1;
                            var childNode = this._addBookmarkNode(node, bookmarks[index], index + 1);
                            this._enumBookmarks(childNode, index, bookmarks);
                        }
                    }
                }
            }
        },
        _enumBookmarks : function (parentNode, index, bookmarks) {
            var jsonBookmark = bookmarks[index];
            if(!jsonBookmark) return;
            var childList = jsonBookmark[BookmarkJSONDataFormat.childs];
            if (childList) {
                var size = childList.length;
                for (var k = 0; k < size; k++) {
                    var index = childList[k] - 1;
                    var childNode = this._addBookmarkNode(parentNode, bookmarks[index], index + 1);
                    this._enumBookmarks(childNode, index, bookmarks);
                }
            }
        },
        _addBookmarkNode : function (parentNode, jsonBookmark, index) {
            var destinationInfo = new WebPDF.PDFData.PDFDestination(jsonBookmark[BookmarkJSONDataFormat.destInfo]);
            var bookmarkInfo = new PDFBookmarkInfo(jsonBookmark[BookmarkJSONDataFormat.parentIndex], jsonBookmark[BookmarkJSONDataFormat.curIndex],
                jsonBookmark[BookmarkJSONDataFormat.title], destinationInfo);
            var bookmarkNode = new PDFBookmark(bookmarkInfo);
            parentNode.addChildBookmark(bookmarkNode);
            return bookmarkNode;
        }
    };

    WebPDF.PDFData.PDFBookmark = PDFBookmark;
    return WebPDF.PDFData.PDFBookmark;
});